import { BasicColumn, FormSchema } from "/@/components/Table"
import { listDataGroupTree } from "/@/api/infra/dataGroup"
import { DataGroupBizType } from "/@/components/BizDataGroup/index"
import { render } from "/@/utils/common/renderUtils"
import { rules } from "/@/utils/helper/validator"
import { DescItem } from "/@/components/Description/index"

const getStatus = (text) => {
  const color = text == "1" ? "green" : text == "0" || text == "2" ? "red" : "gray"
  return render.renderTag(render.renderDict(text, "short_link_status"), color)
}

export const columns: BasicColumn[] = [
  {
    title: "id",
    dataIndex: "id",
    width: 150,
    defaultHidden: true,
  },

  {
    title: "短链",
    dataIndex: "code",
    slots: { customRender: "shortUrl" },
    width: 100,
  },

  {
    title: "名称",
    dataIndex: "title",
    width: 100,
  },

  {
    title: "组名",
    dataIndex: "groupName",
    customRender: ({ text }) => {
      return text ? text : "-"
    },
    width: 100,
  },

  {
    title: "访问数量",
    dataIndex: "accessCount",
    width: 90,
  },
  {
    title: "备注",
    dataIndex: "remark",
    width: 150,
  },

  {
    title: "状态",
    dataIndex: "status",
    width: 60,
    customRender: ({ text }) => {
      return getStatus(text)
    },
  },

  {
    title: "生成时间",
    dataIndex: "createdAt",
    width: 150,
  },
]

export const searchFormSchema: FormSchema[] = [
  {
    field: "title",
    label: "名称",
    component: "Input",
  },
  {
    field: "groupId",
    label: "短链组",
    component: "ApiSelect",
    componentProps: {
      api: listDataGroupTree,
      params: {
        bizType: DataGroupBizType.SHORT_LINK,
      },
      optionFilterProp: "label",
      showSearch: true,
      labelField: "name",
      valueField: "id",
    },
    colProps: { span: 6 },
  },
  {
    label: "状态",
    field: "status",
    component: "JDictSelectTag",
    componentProps: {
      dictType: "short_link_status",
      placeholder: "请选择状态",
    },
  },
]

export const formSchema: FormSchema[] = [
  {
    field: "id",
    label: "id",
    component: "Input",
    show: false,
  },
  {
    field: "code",
    label: "短链",
    component: "Input",
    show: false,
  },

  {
    field: "title",
    label: "名称",
    component: "Input",
    required: true,
  },

  {
    field: "groupId",
    label: "短链组",
    required: true,
    component: "ApiSelect",
    componentProps: {
      api: listDataGroupTree,
      params: {
        bizType: DataGroupBizType.SHORT_LINK,
      },
      optionFilterProp: "label",
      showSearch: true,
      labelField: "name",
      valueField: "id",
    },
  },
  {
    field: "originalUrl",
    label: "原始url",
    rules: rules.rule("http", true),
    component: "Input",
    required: true,
    dynamicDisabled: ({ values }) => {
      return !!values.id
    },
  },

  {
    field: "expireTime",
    label: "过期时间",
    helpMessage: "不填则是永久不过期",
    component: "DatePicker",
    componentProps: {
      showTime: true,
      valueFormat: "YYYY-MM-DD HH:mm:ss",
    },
  },

  {
    label: "状态",
    field: "status",
    component: "JDictSelectTag",
    defaultValue: "1",
    componentProps: {
      dictType: "data_use_status",
      placeholder: "请选择状态",
      type: "radio",
      // stringToNumber: true
    },
    ifShow: ({ values }) => {
      return values.status != "2"
    },
  },

  {
    field: "remark",
    label: "备注",
    component: "InputTextArea",
    componentProps: {
      autoSize: {
        minRows: 3,
        maxRows: 8,
      },
    },
  },
]

export const formDetailSchema: DescItem[] = [
  {
    field: "title",
    label: "名称",
  },
  {
    field: "code",
    label: "短链码",
  },
  {
    field: "shortUrl",
    label: "短链URL",
  },
  {
    field: "groupName",
    label: "短链组",
  },
  {
    field: "originalUrl",
    label: "原始url",
  },
  {
    field: "expireTime",
    label: "过期时间",
    render: (curVal, data) => {
      return curVal ? curVal : "永久"
    },
  },
  {
    field: "status",
    label: "状态",
    render: (curVal, data) => {
      return getStatus(curVal)
    },
  },
  {
    field: "createdAt",
    label: "创建时间",
  },
  {
    field: "updatedAt",
    label: "更新时间",
  },
  {
    field: "createdBy",
    label: "创建者",
  },
  {
    label: "备注",
    labelMinWidth: 60,
    field: "remark",
  },
]
